package com.mc.servlet;

import com.mc.bean.Student;
import com.mc.bean.Teacher;
import com.mc.service.TeacherService;
import com.mc.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zh
 */
@WebServlet("/teacherpass.action")
public class TeacgerPassServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    TeacherService teacherService=new TeacherServiceImpl();
    //获取值
    String oldpass=req.getParameter("oldpass");
    String newpass=req.getParameter("newpass");
    //输出
    System.out.println(oldpass);
    System.out.println(newpass);
    //得到当前登录人的密码
    Teacher teacher=(Teacher) req.getSession().getAttribute("user");
    if(oldpass.equals(teacher.getTpassword())) {
      //设置新密码
      teacher.setTpassword(newpass);
      boolean flag = teacherService.update(teacher);
      System.out.println(flag);
      //重定向
      resp.sendRedirect("userlogin.jsp");
    }else{
      System.out.println("原密码错误");
      req.setAttribute("mess","原密码错误");
      //转发
      req.getRequestDispatcher("teacherpass.jsp").forward(req,resp);
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
  }
}
